/**
 * 
 */
package com.yidin.lsc;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.yidin.entry.MyDynamicEntry;
import com.yidin.entry.MyPhotoDetailAdapter;
import com.yidin.entry.PhotoListEntry;
import com.yidin.entry.StoreDetailEntry;
import com.yidin.logic.MainService;
import com.yidin.logic.Task;
import com.yidin.widget.SlowGallery;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

/**
 * @author cola
 * 
 */
public class NearByStoreDetail extends BaseActivity {
	private Map<Button, View> group;
	private ViewGroup ViewContainer;
	private Button aboutBtn;
	private Button dynamicBtn;
	private Button photoBtn;
	private PhotoAdapter photoAdapter;
	private TextView title;
	private TextView uploadPhoto;
	private ListView dynamicListView;
	private MyDynamicAdapter myDynamicAdapter;
	private StoreDetailEntry l = null;
	private List<MyDynamicEntry> g = null;
	private List<PhotoListEntry> p = null;
	protected LayoutInflater inflater;
	private String AccountId;
	private TextView rankingName;
	private TextView addr;
	private TextView tel;
	private TextView intro;
	private TextView runtime;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dynamic);
		inflater = getLayoutInflater();
		init();
	}

	@Override
	public void init() {
		// TODO Auto-generated method stub
		this.group = new HashMap<Button, View>();
		ViewContainer = (ViewGroup) findViewById(R.id.ViewContainer);
		View view = inflater.inflate(R.layout.storeinfo, null);
		this.title = (TextView) findViewById(R.id.title);
		this.rankingName = (TextView) view.findViewById(R.id.rankingName);
		this.addr = (TextView) view.findViewById(R.id.addr);
		this.tel = (TextView) view.findViewById(R.id.tel);
		this.intro = (TextView) view.findViewById(R.id.intro);
		this.runtime = (TextView) view.findViewById(R.id.runtime);

		ViewContainer.addView(view);
		this.aboutBtn = (Button) findViewById(R.id.ButtonAbout);
		group.put(this.aboutBtn, view);
		this.dynamicBtn = (Button) findViewById(R.id.ButtonDynamic);
		this.photoBtn = (Button) findViewById(R.id.ButtonPhoto);
		((LinearLayout) findViewById(R.id.outRightBtn))
				.setVisibility(View.INVISIBLE);

		this.aboutBtn.setOnClickListener(aboutOnClickListener);
		this.dynamicBtn.setOnClickListener(dynamicOnClickListener);
		this.photoBtn.setOnClickListener(myPhotoOnClickListener);

		Bundle bundle = this.getIntent().getExtras();

		if (bundle != null) {
			AccountId = bundle.getString("AccountId");

			Map obj = new HashMap<String, String>();
			obj.put("AccountId", AccountId);
			obj.put("AccountFrom", lscManager.getInstance().myAccount.name);// AccountId
			showProgress(this);//
			Task task = new Task(Task.TASK_NEARBY_SHOPDETAIL, obj);// TASK_ACCOUNT_DETAIL
			MainService.AddNewTask(task);// TASK_NEARBY_SHOPDETAIL
		}
	}

	private OnClickListener dynamicOnClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			if (group.get(v) == null) {
				group.get(aboutBtn).setVisibility(View.GONE);
				if (group.get(photoBtn) != null)
					group.get(photoBtn).setVisibility(View.GONE);
				View view = inflater.inflate(R.layout.listview, null);
				dynamicListView = (ListView) view
						.findViewById(R.id.myFriendListView);
				group.put((Button) v, view);
				group.get(aboutBtn).setVisibility(View.GONE);
				ViewContainer.addView(view);
			} else {
				((ViewGroup) group.get(aboutBtn)).setVisibility(View.GONE);
				if (group.get(photoBtn) != null)
					group.get(photoBtn).setVisibility(View.GONE);
				group.get(dynamicBtn).setVisibility(View.VISIBLE);
			}

			if (p == null) {
				Map obj = new HashMap<String, String>();
				obj.put("AccountId", AccountId);

				showProgress(NearByStoreDetail.this);
				Task task = new Task(Task.TASK_STOREMYDYNAMIC_LIST, obj);
				MainService.AddNewTask(task);
			} else
				dynamicListView.setAdapter(myDynamicAdapter);
			title.setText("动态");
		}

	};

	private OnClickListener aboutOnClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			for (Map.Entry<Button, View> entry : group.entrySet())
				entry.getValue().setVisibility(View.GONE);
			group.get(v).setVisibility(View.VISIBLE);

			if (l == null) {
				Map obj = new HashMap<String, String>();
				obj.put("AccountId", AccountId);

				showProgress(NearByStoreDetail.this);
				Task task = new Task(Task.TASK_NEARBY_SHOPDETAIL, obj);
				MainService.AddNewTask(task);
			}else{
				rankingName.setText(l.AccountName);
				addr.setText(l.Address);
				tel.setText(l.AccountId);
				intro.setText(l.Content);
				runtime.setText(l.BusinessHours);
			}
			title.setText(l.AccountName);
		}

	};

	private GridView myGridView;
	private ArrayList<PhotoListParcelable> PhotoListParcelableList;
	private OnClickListener myPhotoOnClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			for (Map.Entry<Button, View> entry : group.entrySet())
				entry.getValue().setVisibility(View.GONE);
			if (group.get(v) == null) {// myphotodetail
				View view = inflater.inflate(R.layout.gridview, null);
				group.put((Button) v, view);
				ViewContainer.addView(view);
				myGridView = (GridView) view.findViewById(R.id.blog);
				myGridView.setOnItemClickListener(gridviewOnItemClickListener);

				// LoadImageFilter();
			}
			group.get(v).setVisibility(View.VISIBLE);

			title.setText("相册");
			if (p == null) {
				Map obj = new HashMap<String, String>();
				obj.put("AccountId", AccountId);

				showProgress(NearByStoreDetail.this);
				Task task = new Task(Task.TASK_STORE_PHOTOLIST, obj);
				MainService.AddNewTask(task);
			} else
				myGridView.setAdapter(photoAdapter);
		}

	};

	private OnItemClickListener gridviewOnItemClickListener = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			// TODO Auto-generated method stub
			if (PhotoListParcelableList == null) {
				int len = p.size();
				PhotoListParcelableList = new ArrayList<PhotoListParcelable>();

				for (int n = 0; n < len; n++) {
					PhotoListParcelable pp = new PhotoListParcelable();
					pp.FileDesc = p.get(n).FileDesc;
					pp.FilePath = p.get(n).FilePath;
					pp.Id = p.get(n).Id;
					PhotoListParcelableList.add(pp);
				}
				Intent it = new Intent(NearByStoreDetail.this,
						MyPhotoDetail.class);
				it.putParcelableArrayListExtra("list", PhotoListParcelableList);
				startActivity(it);

			}
		}

	};

	@Override
	public void refresh(Object... param) {
		// TODO Auto-generated method stub
		dismissProgress();
		int task = (Integer) param[0];
		switch (task) {
		case Task.TASK_NEARBY_SHOPDETAIL:
			l = (StoreDetailEntry) param[1];
			if (l == null)
				return;
			if (!l.e.equals("0000"))
				Toast.makeText(this, l.e, Toast.LENGTH_LONG).show();

			rankingName.setText(l.AccountName);
			addr.setText(l.AccountName);
			tel.setText(l.AccountName);
			intro.setText(l.AccountName);
			runtime.setText(l.AccountName);
			break;
		case Task.TASK_STOREMYDYNAMIC_LIST: {
			g = (List<MyDynamicEntry>) param[1];
			if (g == null)
				return;
			myDynamicAdapter = new MyDynamicAdapter(this, g);
			dynamicListView.setAdapter(myDynamicAdapter);
			break;
		}
		case Task.TASK_STORE_PHOTOLIST:
			p = (List<PhotoListEntry>) param[1];
			if (p == null)
				return;
			photoAdapter = new PhotoAdapter(this, p);
			myGridView.setAdapter(photoAdapter);
			break;
		}
	}

	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub

		return false;
	}

}
